package com.seventh.associationsystem.service;

import com.seventh.associationsystem.bean.Role;
import com.seventh.associationsystem.bean.User;
import com.seventh.associationsystem.repository.RoleRepository;
import com.seventh.associationsystem.repository.UserRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;

import javax.annotation.PostConstruct;
import javax.transaction.Transactional;
import java.util.ArrayList;
import java.util.List;

@Service
public class UserService implements UserDetailsService {

    @Autowired
    private UserRepository userRepository;

    @Autowired
    private RoleRepository roleRepository;

    @PostConstruct
    public void before() {
        Role roleAdmin = new Role(Role.ROLE_ADMIN, "ROLE_ADMIN");
        Role roleSchoolManager = new Role(Role.ROLE_SCHOOL_MANAGER, "ROLE_SCHOOL_MANAGER");
        Role roleAssociationManager = new Role(Role.ROLE_ASSOCIATION_MANAGER, "ROLE_ASSOCIATION_MANAGER");
        Role roleStudent = new Role(Role.ROLE_STUDENT, "ROLE_STUDENT");
        roleRepository.save(roleAdmin);
        roleRepository.save(roleSchoolManager);
        roleRepository.save(roleAssociationManager);
        roleRepository.save(roleStudent);

        User admin = new User();
        admin.setUsername("admin");
        admin.setPassword("admin");

        List<Role> roles = new ArrayList<>();
        Role role = roleRepository.getById(Role.ROLE_ADMIN);
        System.out.println(role.getId());
        System.out.println(role.getName());
        roles.add(role);
        admin.setRoles(roles);

        if (userRepository.findByUsername(admin.getUsername()) != null) {
            return;
        }

        BCryptPasswordEncoder encoder = new BCryptPasswordEncoder();
        final String rawPassword = admin.getPassword();
        admin.setPassword(encoder.encode(rawPassword));
        userRepository.save(admin);
    }


    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        User user = userRepository.findByUsername(username);
        if (user == null) {
            throw new UsernameNotFoundException("用户名不存在");
        }
        return user;
    }

    public User getUserByUsername(String username) {
        return userRepository.findByUsername(username);
    }

    @Transactional
    public void addUser(User user) {
        userRepository.save(user);
    }

    public void update(User user) {
        userRepository.save(user);
    }

//    public Page<User> getAllPageable(Pageable pageable, GrantedAuthority auth) {
//        return userRepository.getAllByAuthoritiesContaining(pageable, auth);
//    }

    public List<User> getAllUsers() {
        return userRepository.findAll();
    }

    public void deleteByUid(String uid) {
        userRepository.delete(uid);
    }

    public User getCurrentUser() {
        return userRepository.findByUsername(((UserDetails) SecurityContextHolder.getContext().getAuthentication().getPrincipal()).getUsername());
    }
}

